{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from paddlehub.datasets.base_nlp_dataset import TextClassificationDataset\n",
    "class MyDataset(TextClassificationDataset):\n",
    "    base_path='data'\n",
    "    label_list=['0.0','1.0','2.0','3.0','4.0','5.0','6.0','7.0']\n",
    "    def __init__(self,tokenizer,max_seq_len:int=128,mode:str='train'):\n",
    "        if mode=='train':\n",
    "            data_file='train.txt'\n",
    "        elif mode=='test':\n",
    "            data_file='test.txt'\n",
    "        else:\n",
    "            data_file='dev.txt'\n",
    "        super().__init__(\n",
    "        base_path=self.base_path,\n",
    "        tokenizer=tokenizer,\n",
    "        max_seq_len=max_seq_len,\n",
    "        mode=mode,\n",
    "        data_file=data_file,label_list=self.label_list,\n",
    "        is_file_with_header=False)\n",
    "import paddlehub as hub\n",
    "model = hub.Module(name= 'ernie_tiny' ,task='seq-cls', num_classes=len(MyDataset.label_list))\n",
    "tokenizer=model.get_tokenizer()\n",
    "train_dataset=MyDataset(tokenizer)\n",
    "test_dataset=MyDataset(tokenizer,model='test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle\n",
    "optimizer = paddle.optimizer.Adam(learning_rate=5e-5,parameters=model.parameters())\n",
    "trainer = hub.Trainer(model,optimizer,checkpoint_dir='./ckpt',use_gpu=true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trainer.train(train_dataset,epochs=3,bath_size=32,eval_dataset=dev_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trainer.evaluate(test_data_set,batch_size=32)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
